package com.woniuxy.yoga.progress.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.progress.dao.model.Lifecard;
import com.woniuxy.yoga.progress.model.form.CreateLifeCardForm;
import com.woniuxy.yoga.progress.model.param.CreateLifeCardParam;
import com.woniuxy.yoga.progress.service.LifecardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 终身卡流程表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/lifecard")
@Api(tags = "终身卡进度管理")
@Validated
public class LifecardController {

    @Autowired
    private LifecardService lifecardService;

    /**
     * 通过用户id查询终身卡进度
     * @param userId 用户id
     * @return Lifecard
     */
    @GetMapping("/getByUserId/{userId}")
    @ApiOperation("通过用户id查询终身卡进度")
    @YogaLog("通过用户id查询终身卡进度")
    public Result<Lifecard> getByUserId(@PathVariable("userId") @Valid @NotNull Integer userId){
        Lifecard lifecard = lifecardService.getByUserId(userId);
        return Result.getResult(Lifecard.class).setData(lifecard).setMessage("查询成功");
    }

    /**
     * 创建终身卡进度表
     * @param form 创建终身卡进度表参数
     * @return String
     */
    @PostMapping("/creatLifeCard")
    @ApiOperation("创建终身卡进度表")
    @YogaLog("创建终身卡进度表")
    public Result<?> createLifeCard(@RequestBody @Valid CreateLifeCardForm form){
        lifecardService.createLifeCard(BeanUtil.toBean(form,CreateLifeCardParam.class));
        return Result.getResult().setMessage("创建终身卡进度表成功");
    }

    /**
     * 转让终身卡
     * @param transUserId 被转用户id
     * @return string
     */
    @GetMapping("/transformLifeCard/{transUserId}")
    @ApiOperation("转让终身卡")
    @YogaLog("转让终身卡")
    public Result<String> transformLifeCard(@PathVariable("transUserId") @Valid @NotNull Integer transUserId,@RequestHeader String authen_token){
        Integer userId = JWTUtil.getUserId(authen_token);
        lifecardService.transformLifeCard(transUserId,userId);
        return Result.getResult(String.class).setMessage("转让成功");
    }

    /**
     * 终身卡次数加1
     * @return string
     */
    @GetMapping("/addLifeCardCount/{lifecardId}")
    @ApiOperation("终身卡次数加1")
    @YogaLog("终身卡次数加1")
    public Result<String> addLifeCardCount(@PathVariable("lifecardId") @Valid @NotNull Integer lifecardId) throws Exception {
        lifecardService.addLifeCardCount(lifecardId);
        return Result.getResult(String.class).setMessage("操作成功");
    }

    /**
     * 终身卡次数减1
     * @return string
     */
    @GetMapping("/reduceLifeCardCount/{lifecardId}")
    @ApiOperation("终身卡次数减1")
    @YogaLog("终身卡次数减1")
    public Result<String> reduceLifeCardCount(@PathVariable("lifecardId") @Valid @NotNull Integer lifecardId) throws Exception {
        lifecardService.reduceLifeCardCount(lifecardId);
        return Result.getResult(String.class).setMessage("操作成功");
    }

}

